# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=mesa
pkgver=21.3.9
pkgrel=2
pkgdesc="Mesa DRI OpenGL library"
url="https://www.mesa3d.org"
arch="all"
license="MIT SGI-B-2.0 BSL-1.0"
subpackages="
	$pkgname-dbg
	$pkgname-dev
	$pkgname-dri-classic:_dri
	$pkgname-dri-gallium:_dri
	$pkgname-va-gallium:_va
	$pkgname-vdpau-gallium:_vdpau
	$pkgname-glapi $pkgname-egl $pkgname-gl $pkgname-gles
	$pkgname-xatracker $pkgname-osmesa $pkgname-gbm
	$pkgname-vulkan-ati:_vulkan
	$pkgname-dri-ati:_dri_deprecated
	$pkgname-dri-nouveau:_dri_deprecated
	$pkgname-vulkan-layers:_vulkan_layers
	"
_llvmver=14
depends_dev="
	libdrm-dev
	libxext-dev
	libxdamage-dev
	libxcb-dev
	libxshmfence-dev
	"
makedepends="
	$depends_dev
	bison
	eudev-dev
	expat-dev
	findutils
	flex
	gettext
	elfutils-dev
	glslang-dev
	libtool
	libxfixes-dev
	libva-dev
	libvdpau-dev
	libx11-dev
	libxml2-dev
	libxrandr-dev
	libxvmc-dev
	libxxf86vm-dev
	llvm$_llvmver-dev
	meson
	py3-mako
	python3
	wayland-dev
	wayland-protocols
	xorgproto
	zlib-dev
	zstd-dev
	"
source="
	https://mesa.freedesktop.org/archive/mesa-$pkgver.tar.xz
	musl-fix-includes.patch
	add-use-elf-tls.patch
	disable-rgb10-by-default.patch
	0001-radeonsi-On-Aarch64-force-persistent-buffers-to-GTT.patch
	"
replaces="mesa-dricore"
options="!check" # we skip tests intentionally

if [ "$CARCH" = x86 ]; then
	# Bug upstream that needs to be fixed
	options="$options textrels"
fi

_dri_driverdir=/usr/lib/xorg/modules/dri
_dri_drivers="r100,r200,nouveau"
_gallium_drivers="r300,r600,radeonsi,nouveau,swrast,virgl"
_gallium_provides="
	$pkgname-dri-swrast=$pkgver-r$pkgrel
	$pkgname-dri-virtio=$pkgver-r$pkgrel
	"
_vulkan_drivers="amd"
_vulkan_layers="device-select,overlay"
case "$CARCH" in
x86*)
	_dri_drivers="${_dri_drivers},i915,i965"
	_gallium_drivers="${_gallium_drivers},svga,iris,crocus"
	_vulkan_drivers="${_vulkan_drivers},intel"
	_vulkan_layers="$_vulkan_layers,intel-nullhw"
	subpackages="
			$subpackages
			$pkgname-dri-intel:_dri_deprecated
			$pkgname-vulkan-intel:_vulkan
			"
	_gallium_provides="$_gallium_provides $pkgname-dri-vmwgfx=$pkgver-r$pkgrel"
	_arch_opts="-Ddri3=enabled"
	case "$CARCH" in
	x86)
		_arch_opts="$_arch_opts -Dglx-read-only-text=true";;
	esac
	;;
armhf|armv7|aarch64)
	_gallium_drivers="${_gallium_drivers},vc4,v3d,freedreno,kmsro,lima,panfrost,etnaviv,tegra"
	_gallium_provides="$_gallium_provides
		$pkgname-dri-vc4=$pkgver-r$pkgrel
		$pkgname-dri-v3d=$pkgver-r$pkgrel
		$pkgname-dri-freedreno=$pkgver-r$pkgrel
		$pkgname-dri-kmsro=$pkgver-r$pkgrel
		$pkgname-dri-lima=$pkgver-r$pkgrel
		$pkgname-dri-panfrost=$pkgver-r$pkgrel
		$pkgname-dri-etnaviv=$pkgver-r$pkgrel
		$pkgname-dri-tegra=$pkgver-r$pkgrel
		"
	case "$CARCH" in
	aarch64)
		_vulkan_drivers="${_vulkan_drivers},broadcom,freedreno,panfrost"
		subpackages="
			$subpackages
			$pkgname-vulkan-broadcom:_vulkan
			$pkgname-vulkan-freedreno:_vulkan
			$pkgname-vulkan-panfrost:_vulkan
			"
		;;
	esac
	;;
esac

build() {
	export CFLAGS="$CFLAGS -D_XOPEN_SOURCE=700"
	export MESA_GIT_SHA1_OVERRIDE=53b2b224dc2de982c37915a0ad218e33365ff75e

	python3 bin/git_sha1_gen.py --output include/git_sha1.h

	abuild-meson \
		-Ddri-drivers-path=$_dri_driverdir \
		-Dgallium-drivers=$_gallium_drivers \
		-Ddri-drivers=$_dri_drivers \
		-Dvulkan-drivers=$_vulkan_drivers \
		-Dvulkan-layers=$_vulkan_layers \
		-Dplatforms=x11,wayland \
		-Dllvm=enabled \
		-Dshared-llvm=enabled \
		-Dshared-glapi=enabled \
		-Dgbm=enabled \
		-Dglx=dri \
		-Dopengl=true \
		-Dosmesa=true \
		-Dgles1=disabled \
		-Dgles2=enabled \
		-Degl=enabled \
		-Dgallium-xa=enabled \
		-Dgallium-vdpau=enabled \
		-Dgallium-va=enabled \
		-Dgallium-xvmc=disabled \
		-Dgallium-nine=false \
		-Db_ndebug=true \
		$_arch_opts \
		. output

	# Print config
	meson configure output

	# parallel build workaround
	ninja -C output src/compiler/nir/nir_intrinsics.h

	meson compile ${JOBS:+-j ${JOBS}} -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
}

egl() {
	pkgdesc="Mesa libEGL runtime libraries"
	depends="mesa=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libEGL.so* "$subpkgdir"/usr/lib/
}

gl() {
	pkgdesc="Mesa libGL runtime libraries"
	depends="mesa=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libGL.so* "$subpkgdir"/usr/lib/
}

glapi() {
	pkgdesc="Mesa shared glapi"
	replaces="$pkgname-gles=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libglapi.so.* "$subpkgdir"/usr/lib/
}

gles() {
	pkgdesc="Mesa libGLESv2 runtime libraries"
	depends="mesa=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libGLES*.so* "$subpkgdir"/usr/lib/
}

xatracker() {
	pkgdesc="Mesa XA state tracker for vmware"
	depends="mesa=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libxatracker*.so.* "$subpkgdir"/usr/lib/
}

osmesa() {
	pkgdesc="Mesa offscreen rendering libraries"
	depends="mesa=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libOSMesa.so.* "$subpkgdir"/usr/lib/
}

gbm() {
	pkgdesc="Mesa gbm library"
	depends="mesa=$pkgver-r$pkgrel"

	install -d "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libgbm.so.* "$subpkgdir"/usr/lib/
}

# Move links referencing the same file to the subpackage.
# Usage: _mv_links <base directory> <example>
# where <example> is one of the libraries covered by the megadriver.
# The example is used to find other links that point to the same file.
_mv_links() {
	install -d "$subpkgdir"/$1
	find -L "$pkgdir"/$1 -samefile "$pkgdir"/$1/$2 -print0 \
		| xargs -0 -I{} mv {} "$subpkgdir"/$1/
}

_mv_vulkan() {
	local i
	install -d "$subpkgdir"/usr/lib
	install -d "$subpkgdir"/usr/share/vulkan/icd.d
	for i in "$@"; do
		mv "$pkgdir"/usr/lib/libvulkan_$i.so "$subpkgdir"/usr/lib/
		mv "$pkgdir"/usr/share/vulkan/icd.d/${i}* "$subpkgdir"/usr/share/vulkan/icd.d/
	done
}

# Mesa uses "megadrivers" where multiple drivers are linked into one shared
# library. This library is then hard-linked to separate files (one for each driver).
# Each subpackage contains one megadriver so that all the hard-links are preserved.

_dri() {
	local n=${subpkgname##*-dri-}
	pkgdesc="Mesa $n DRI drivers"
	depends="mesa=$pkgver-r$pkgrel"

	case $n in
	classic)
		# libmesa_dri_drivers.so
		_mv_links $_dri_driverdir radeon_dri.so ;;
	gallium)
		# libgallium_dri.so
		_mv_links $_dri_driverdir swrast_dri.so
		provides="$_gallium_provides"
		;;
	esac
}

# Compatibility packages for upgrading from the old (driver-specific) subpackages
_dri_deprecated() {
	local n=${subpkgname##*-dri-}
	pkgdesc="Mesa DRI drivers for $n (DEPRECATED)"
	depends="
		mesa-dri-classic=$pkgver-r$pkgrel
		mesa-dri-gallium=$pkgver-r$pkgrel
		"

	case $n in
	ati|nouveau)
		depends="$depends mesa-va-gallium mesa-vdpau-gallium" ;;
	esac

	mkdir "$subpkgdir"
}

_va() {
	local n=${subpkgname##*-va-}
	pkgdesc="Mesa $n VAAPI drivers"
	depends="mesa=$pkgver-r$pkgrel libva"

	case $n in
	gallium)
		# libgallium_drv_video.so
		_mv_links /usr/lib/dri radeonsi_drv_video.so ;;
	esac
}

_vdpau() {
	local n=${subpkgname##*-vdpau-}
	pkgdesc="Mesa $n VDPAU drivers"
	depends="mesa=$pkgver-r$pkgrel libvdpau"

	case $n in
	gallium)
		# libvdpau_gallium.so.1.0.0
		_mv_links /usr/lib/vdpau libvdpau_radeonsi.so.1.0.0 ;;
	esac
}

_vulkan() {
	local n=${subpkgname##*-vulkan-}
	pkgdesc="Mesa Vulkan API driver for $n"
	depends="mesa=$pkgver-r$pkgrel"

	case $n in
	ati)
		_mv_vulkan radeon ;;
	intel)
		_mv_vulkan intel ;;
	broadcom)
		_mv_vulkan broadcom ;;
	freedreno)
		_mv_vulkan freedreno ;;
	panfrost)
		_mv_vulkan panfrost ;;
	esac
}

_vulkan_layers() {
	pkgdesc="collection of vulkan layers from mesa"
	depends="python3"

	# Remove this after the release of the next stable (3.14)
	# it originally was claed layer as it only packaged the
	# overlay one but now it also packages device-select and
	# intel-nullhw (on x86*)
	provides="$pkgname-vulkan-layer=$pkgver-r$pkgrel"
	replaces="$pkgname-vulkan-layer=$pkgver-r$pkgrel"

	amove usr/share/vulkan/explicit_layer.d
	amove usr/share/vulkan/implicit_layer.d
	amove usr/lib/libVkLayer_*.so
	amove usr/bin/mesa-overlay-control.py
}

sha512sums="
4cec6f4f50f444fcd327f7c40f8899c2f265e348e121455262b540b1f890a1468bbea59965af0876c548fa97aa0a05a1b23fa6ca7d308bd60328cfdeab757684  mesa-21.3.9.tar.xz
77b40d3b70a78e1f261f0a0358f3b5f15d5f0a6d51ba317e38053f426a14487a459743df2e4734e9c086f1f083572e78279eb8a5dde191ef0c2914594513194b  musl-fix-includes.patch
de7c6fec14476539c564afa4bf59a3c8d5f34a687ffdbacfe94403e3ad4e78773222b2b74b08a1fd16b7dc9504decca1429273c179ad702c02a140e1dff15205  add-use-elf-tls.patch
ae5ffb9259efc339a868c778186889fa1a6eacb4dc4b2452f31f9c65e1a6273e856665300d2982fb2680b4d182aae574ceda258e15c00d7ae16960ba09240ef1  disable-rgb10-by-default.patch
fdbc13888f09d157feae6ce3b8a63cc606d7ed19e364cdb89ccce25a367255b498bbe82862a487c29d8f98d5e31980ef1f0879b06823cedb54801fd54cd461ad  0001-radeonsi-On-Aarch64-force-persistent-buffers-to-GTT.patch
"
